﻿Imports DTO
Imports Business



Public Class frmListEmployee

    Dim bsOrganization As BSOrganization

    Dim dtOrgan As DataTable

    Dim bsEmployee As BSEmployee

    Dim dtEmployee As DataTable

    ' flagEmp = true if datagridview is displaying employee was registered.
    Dim FlagEmp As Boolean

    Dim bsFKAttend As BSFKAttend

    Dim serverName As String

    Public Sub New(ByVal serverName As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.serverName = serverName

        bsOrganization = New BSOrganization(Me.serverName)

        bsEmployee = New BSEmployee(Me.serverName)

        bsFKAttend = New BSFKAttend(Me.serverName)

        ' get datatable all organization
        dtOrgan = bsOrganization.GetOrganization()

        frmCompanyDepartment.LoadTreeOrgan(dtOrgan, trvOrgan)

        trvOrgan.ExpandAll()

        dtEmployee = New DataTable()

    End Sub




    Private Sub trvOrgan_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvOrgan.AfterSelect

        FlagEmp = True

        'load data gridview
        LoadGridView(e.Node)
       

    End Sub


    Private Sub LoadGridView(ByVal node As TreeNode)

        ' show list of employees on datagridview
        'get employees of node is selected
        Dim dsEmployee As DataSet = New DataSet()

        dsEmployee.Tables.Add(bsEmployee.GetEmployee(node.Name, "%"))

        ' get employees of children node of node is selected
        GetEmpChildNode(node.Name, dtOrgan, "%", dsEmployee)

        ' merge all tables in dataset to one table
        dtEmployee = dsEmployee.Tables.Item(0).Copy()

        For i As Integer = 1 To dsEmployee.Tables.Count - 1

            dtEmployee.Merge(dsEmployee.Tables.Item(i))

        Next

        'set datasource to datagridview
        grdEmployee.DataSource = bsEmployee.GetDataSource(dtEmployee)

        'hide column status
        grdEmployee.Columns.Item("Status").Visible = False

        ' clear selection
        grdEmployee.ClearSelection()

        FillColorGrdEmployee()

    End Sub


    ''' <summary>
    ''' Check checkbox show deleted record if it is selected and fill color or hide record is deleted
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub FillColorGrdEmployee()

        ' if checkbox show deleted record is checked
        If chkDeleted.Checked Then

            ' fill background color record is deleted
            For Each gridViewRow As DataGridViewRow In grdEmployee.Rows

                If gridViewRow.Cells.Item("Status").Value.ToString().Equals("1") Then

                    gridViewRow.Visible = True
                    gridViewRow.DefaultCellStyle.BackColor = Color.DarkGray

                End If

            Next

        Else
            '
            Dim cm As CurrencyManager = BindingContext(grdEmployee.DataSource)

            ' hide record is deleted
            For Each gridViewRow As DataGridViewRow In grdEmployee.Rows

                If gridViewRow.Cells.Item("Status").Value.ToString().Equals("1") Then

                    cm.SuspendBinding()

                    gridViewRow.Visible = False

                    cm.ResumeBinding()

                End If

            Next

        End If

    End Sub


    Private Sub grdEmployee_Sorted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grdEmployee.Sorted

        FillColorGrdEmployee()

    End Sub

    Private Sub chkDeleted_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDeleted.CheckedChanged

        If grdEmployee.Rows.Count > 0 Then

            FillColorGrdEmployee()

        End If

    End Sub

    Private Sub btnLoadReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadReg.Click

        FlagEmp = False

        ' load data registration from FP terminal and add to Terminal user data
        lblMessage.Text = "Working..."
        System.Windows.Forms.Application.DoEvents()

        'Test get all user data of FP terminal registration (successfull!)
        bsFKAttend.SaveUserData()

        lblMessage.Text = "Load success"
        System.Windows.Forms.Application.DoEvents()


        ' load data haven't register in Employee from Terminal user data
        grdEmployee.DataSource = bsEmployee.GetTableIdNotReg()

    End Sub



    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        ' lblMessage.Text = FlagEmp

        ' if select node
        If FlagEmp Then

            Dim strSearch As String

            If Not String.IsNullOrEmpty(txtSearch.Text) Then

                strSearch = txtSearch.Text.Trim()

            Else

                strSearch = "%"

            End If

            'get employees of node is selected
            Dim dsEmployee As DataSet = New DataSet()

            dsEmployee.Tables.Add(bsEmployee.GetEmployee(trvOrgan.SelectedNode.Name, strSearch))

            ' get employees of children node of node is selected
            GetEmpChildNode(trvOrgan.SelectedNode.Name, dtOrgan, strSearch, dsEmployee)

            ' merge all tables in dataset to one table
            Dim dt As DataTable = dsEmployee.Tables.Item(0).Copy()

            For i As Integer = 1 To dsEmployee.Tables.Count - 1

                dt.Merge(dsEmployee.Tables.Item(i))

            Next

            'set datasource to datagridview
            grdEmployee.DataSource = bsEmployee.GetDataSource(dt)

            'hide column status
            grdEmployee.Columns.Item("Status").Visible = False

            ' clear selection
            grdEmployee.ClearSelection()

            FillColorGrdEmployee()

        End If

    End Sub


    ''' <summary>
    ''' get employee of children of Node by no, name
    ''' </summary>
    ''' <param name="supNodeKey"></param>
    ''' <param name="dtNode"></param>
    ''' <param name="str">string search</param>
    ''' <param name="ds"></param>
    ''' <remarks></remarks>
    Public Sub GetEmpChildNode(ByVal supNodeKey As String, ByVal dtNode As DataTable, ByVal str As String, ByVal ds As DataSet)

        For Each row As DataRow In dtNode.Rows

            If row.Item("SUP_ORGANIZATIONAL_NO").ToString().Equals(supNodeKey) Then

                'add employee in child node to dataset
                ds.Tables.Add(bsEmployee.GetEmployee(row.Item("ORGANIZATIONAL_NO"), str))

                ' recur to get employee in child node
                GetEmpChildNode(row.Item("ORGANIZATIONAL_NO"), dtNode, str, ds)

            End If

        Next

    End Sub


    Private Sub grdEmployee_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdEmployee.CellDoubleClick


        '  lblMessage.Text = grdEmployee.CurrentRow.Cells.Item("Status").Value



        ' check if it is data from data employee has registered or data registration from FP terminal
        If FlagEmp Then

            ' check if it has not delete
            If grdEmployee.CurrentRow.Cells.Item("Status").Value.ToString().Equals("0") Then

                ' get ID and No and organization
                Dim frmEmployeeDetail As frmEmployeeDetail = New frmEmployeeDetail(Me.serverName)

                ' get ID
                frmEmployeeDetail.IdProperty = grdEmployee.CurrentRow.Cells.Item(0).Value

                ' get No
                frmEmployeeDetail.EmpNoProperty = grdEmployee.CurrentRow.Cells.Item(1).Value

                ' show form
                If frmEmployeeDetail.ShowDialog() = DialogResult.OK Then

                    ' reload datagridview
                    LoadGridView(trvOrgan.SelectedNode)

                End If

            End If

            ' data from FP terminal
        Else

            Dim frmEmployeeDetail As frmEmployeeDetail = New frmEmployeeDetail(Me.serverName)

            ' get ID 
            frmEmployeeDetail.IdProperty = grdEmployee.CurrentRow.Cells.Item(0).Value

            'set no = null
            frmEmployeeDetail.EmpNoProperty = ""

            ' show form
            If frmEmployeeDetail.ShowDialog() = DialogResult.OK Then
                
                ' load data haven't register in Employee from Terminal user data
                grdEmployee.DataSource = bsEmployee.GetTableIdNotReg()

            End If


        End If




        'Dim frmEmployeeDetail As frmEmployeeDetail = New frmEmployeeDetail()

        'frmEmployeeDetail.ShowDialog()


    End Sub


    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

        frmMain.RemoveTabpage()

    End Sub

End Class